package com.ugrow.ugrowcommunity.activity.warranty;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.databinding.ListItemLayoutBinding;

import java.util.List;


class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<NoteBean> mNoteList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private MyOpenHelper myOpenHelper;

    public MyAdapter(Context context, List<NoteBean> mNoteList) {
        this.mContext = context;
        this.mNoteList = mNoteList;
        mLayoutInflater = LayoutInflater.from(mContext);
        myOpenHelper = new MyOpenHelper(mContext);

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.list_item_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
        NoteBean noteBean = mNoteList.get(position);
        holder.binding.tvTitle.setText(noteBean.getTitle());
        holder.binding.tvContent.setText(noteBean.getContent());
        holder.binding.tvTime.setText(noteBean.getCreatedTime());
        if (noteBean.isSuccessful()) {
            holder.binding.repairing.setVisibility(View.GONE);
            holder.binding.repairSuccess.setVisibility(View.VISIBLE);
        } else {
            holder.binding.repairing.setVisibility(View.VISIBLE);
            holder.binding.repairSuccess.setVisibility(View.GONE);
        }
        holder.binding.rtlItemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CatActivity.class);
                intent.putExtra("noteBean", noteBean);
                mContext.startActivity(intent);
            }
        });

        //长按弹出删除或编辑
        holder.binding.rtlItemContainer.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Dialog dialog = new Dialog(mContext);
                View view = mLayoutInflater.inflate(R.layout.list_item_dialog_layout, null);

                TextView tvDelete = view.findViewById(R.id.tv_delete);
                TextView tvEdit = view.findViewById(R.id.tv_edit);

                tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int row = myOpenHelper.deleteDataById(noteBean.getId());
                        if (row > 0) {
                            removeData(position);
                            Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(mContext, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                    }
                });

                tvEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, EditActivity.class);
                        intent.putExtra("noteBean", noteBean);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                });

                dialog.setContentView(view);
                dialog.show();
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNoteList.size();
    }

    public void refreshData(List<NoteBean> noteBeans) {
        this.mNoteList = noteBeans;
        notifyDataSetChanged();
    }

    public void removeData(int position) {
        mNoteList.remove(position);
        notifyItemRemoved(position);

    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ListItemLayoutBinding binding;

        public MyViewHolder(View itemView) {
            super(itemView);
            binding = ListItemLayoutBinding.bind(itemView);

        }
    }
}
